package com.blackline.auto.Import;

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

import com.blackline.auto.common.CommonMethods;
import com.blackline.declareUI.UI;
import com.blackline.parameters.Parameters;
import com.blackline.parameters.TestData;

/**
 * @author XBO
 * @version Feb, 2013
 * <p>The smoke test to Import module.
 */
public class Import_smoketest {
	
	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger=Logger.getLogger(Import_smoketest.class);
	UI UI=new UI();
	Parameters parameters;
	@Before
	public void setUp() throws Exception {
		FirefoxProfile profile=new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters=new Parameters("parameters.xml");

		driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		UI.declareAllUI(driver);
	}
	
	@Test
	public void Accountsmoketest() throws Exception {
		TestData testdata=new TestData("TestData/Import_smoketest.xml");
		int sleepTime = 5000;
		
		UI.LogonPage.Login(driver, parameters.urlDev3, parameters.getLogonID("admin"), parameters.getPassword("admin"));
		UI.NavigationMenu.IMPORTS.click();
		UI.NavigationMenu.Import_Data.click();
		Thread.sleep(sleepTime);
		verificationErrors.append(UI.page.VerifyPageTitle(driver, "Import Data from a file"));
		UI.page.selectOptionForDropdownList(UI.ImportData.Import_dropDownList, "Accounts");
		UI.ImportData.Document_textField.sendKeys(testdata.getInput("imprtFile"));
		UI.ImportData.Import_button.click();
		Thread.sleep(sleepTime);
		
		String result = UI.ImportData.OperationResult.getText();
		logger.info(result);
		if (!result.contains("Fail")) //Expected msg: Operation Failed: Incorrect DAI Configuration
		{
			logger.info("Result is as expected: 'Operation Succeeded'.");
		}
		else
		{
			logger.info("Result is NOT as expected. Should be 'Operation Succeeded', but is: Failed");
			verificationErrors.append("Result is NOT as expected. Should be 'Operation Succeeded', but is: Failed");
		}
		
		UI.SessionMenu.LogOut.click();
	}
	
	@After
	public void tearDown() throws Exception {
		CommonMethods.takeScreenShot(this.getClass(), driver);
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
}